Templates
All the relevant steps and tasks are defined in the template. Do not include hard-coded values inside the template, so it can be re-used for other pipelines.
Template structure
To use a file as a template, the file must be able to receive values, passed from the calling pipeline.
Parameters
- stage: STAGE_PROVISION_TEST variables: - template: 'variables/pipeline-variables-tst.yml' jobs: - template: 'templates/template-infra.yaml' parameters: environment: '${{ variables.environment }}' appName: '${{ variables.appName }}' appTier: '${{ variables.appTier }}'
As you can see, the values for environment
, appName
and appTier
(set in de variale file) are passed to the template.
parameters: environment: '' appName: '' appTier: ''jobs: - deployment: 'CreateResources' displayName: "Create Resources" environment: '${{ parameters.environment }}' strategy: runOnce: deploy: steps:
Parameter options
In the example above, the environment
is passed as a simple string value. It is possible to add objects to the template itself. This way, it is possible to have nested data send to the template itself. When you're dealing with complex solutions and infrastructure this might be a huve saver.
- stage: STAGE_PROVISION_TEST variables: - template: 'variables/pipeline-variables-tst.yml' jobs: - template: 'templates/template-infra.yaml' parameters: environment: '${{ variables.environment }}' appName: '${{ variables.appName }}' appTier: '${{ variables.appTier }}' brands: - brand: name: brandA fullName: Full Brand Name A clients: - client: name: consumer - client: name: dealer - brand: name: brandB fullName: Full Brand Name B clients: - client: name: consumer - client: name: dealer - brand: name: brandC fullName: Full Brand Name C clients: - client: name: consumer - client: name: dealer
In this example, brands
is passed as a parameter that holds a complex data object. You need to pass brands
only as a parameter. The complete nested object is send to the template.
The template just requires the brand
parameter defined:
parameters: environment: '' appName: '' appTier: '' brand: ''
Using complex parameters
Once the data object is passed to the template, the values can easily be retreived.
parameters: environment: '' appName: '' appTier: '' brand: ''jobs: - ${{ each brand in parameters.brands }}: - ${{ each client in brand.clients }}: - deployment: 'CreateResources' displayName: "Create Resources" environment: '${{ parameters.environment }}' strategy: runOnce: deploy: steps: - checkout: self - task: AzureCLI@2 displayName: 'Create resources for ${{ brand.name }}_${{ client.name }}'